var express = require('express');
var router = express.Router();
var sqlite3 = require('sqlite3').verbose();
var path = require('path');
/* GET users listing. */
router.get('/', function (req, res, next) {
    var db = new sqlite3.Database(path.join(__dirname, "../db/Delphi.s3db"));
    var mode = req.query.mode;
    if (mode == '0') { //专家自我评价
        if (req.query.type == '1') { //学生
            var s_title = 0.4 * parseFloat(req.query.s_title);
            var v_time = 0.5 * parseFloat(req.query.v_time);
            var c_time = 0.1 * parseFloat(req.query.c_time);
            var sqlStr = "UPDATE s_expert SET s_title = ?,v_time=?,c_time=? where s_expert_id = ?";
            try {
                db.all(sqlStr, [s_title, v_time, c_time, req.session.username], function (err) {
                    if (err != null) {
                        res.send('0');
                    } else {
                        res.send('1');
                    }
                });
            } catch (ex) {
                console.log(ex);
            }
        } else { //职业人士
            var w_place = 0.2 * parseFloat(req.query.w_place);
            var w_time = 0.1 * parseFloat(req.query.w_time);
            var edu = 0.1 * parseFloat(req.query.edu);
            var field = 0.3 * parseFloat(req.query.field);
            var age = 0.1 * parseFloat(req.query.age);
            var p_title = 0.2 * parseFloat(req.query.p_title);
            var sqlStr = "UPDATE p_expert SET w_place = ?,w_time=?,edu=?,field=?,age=?,p_title=? where p_expert_id = ?";
            try {
                db.all(sqlStr, [w_place, w_time, edu, field, age, p_title, req.session.username], function (err) {
                    if (err != null) {
                        res.send('0');
                    } else {
                        res.send('1');
                    }
                });
            } catch (ex) {
                console.log(ex);
            }
        }
    } else if (mode == '1') { //第一轮问卷调查
        var sqlStr = "select * from answer1 where expert_id = ?";
        try {
            db.all(sqlStr, [req.session.username], function (err, rows) {
                if (rows.length != 0) {
                    var sqlStr2 = "UPDATE answer1 SET area = ?,des=?,hs=?,out=?,hs_imp=?,out_imp=?,adv=? where expert_id = ?";
                    db.all(sqlStr2, [req.query.area, req.query.des, req.query.hs, req.query.out, req.query.hs_imp, req.query.out_imp, req.query.adv, req.session.username], function (err) {
                        if (err != null) {
                            res.send('0');
                        } else {
                            res.send('1');
                        }
                    });
                } else {
                    var sqlStr2 = "insert into answer1 values(?,?,?,?,?,?,?,?) ";
                    db.all(sqlStr2, [req.session.username, req.query.area, req.query.des, req.query.hs, req.query.out, req.query.hs_imp, req.query.out_imp, req.query.adv], function (err) {
                        if (err != null) {
                            console.log(err);
                            res.send('0');
                        } else {
                            res.send('1');
                        }
                    });
                }
            });
        } catch (ex) {
            console.log(ex);
        }
    } else if (mode == '2') {
        var sqlStr = "select * from answer2_1 where expert_id = ?";
        try {
            db.all(sqlStr, [req.session.username], function (err, rows) {
                if (rows.length != 0) {
                    var sqlStr1 = "UPDATE answer2_1 SET n21 = ?,n14=?,n7=?,fam=? where expert_id = ?";
                    var sqlStr2 = "UPDATE answer2_2 SET g147 = ?,g77=?,g07=?,gno=?,fam=? where expert_id = ?";
                    var sqlStr3 = "UPDATE impact SET analysis = ?,experience=?,know=?,intuition=? where expert_id = ? and q_no='2_1'";
                    var sqlStr4 = "UPDATE impact SET analysis = ?,experience=?,know=?,intuition=? where expert_id = ? and q_no='2_2'";
                    db.all(sqlStr1, [req.query.n21, req.query.n14, req.query.n7, req.query.fam1, req.session.username], function (err) {
                        if (err == null) {
                            db.all(sqlStr2, [req.query.g147, req.query.g77, req.query.g07, req.query.gno, req.query.fam2, req.session.username], function (err) {
                                if (err == null) {
                                    db.all(sqlStr3, [req.query.analysis, req.query.experience, req.query.know, req.query.intuition, req.session.username], function (err) {
                                        if (err == null) {
                                            db.all(sqlStr4, [req.query.analysis2, req.query.experience2, req.query.know2, req.query.intuition2, req.session.username], function (err) {
                                                if (err == null)
                                                    res.send('1');
                                                else {
                                                    res.send('0');
                                                    console.log(err);
                                                }
                                            });
                                        } else
                                            console.log(err);
                                    });
                                } else
                                    console.log(err);
                            });
                        } else
                            console.log(err);
                    });

                } else {
                    var sqlStr1 = "insert into answer2_1 values(?,?,?,?,?,?) ";
                    var sqlStr2 = "insert into answer2_2 values(?,?,?,?,?,?,?) ";
                    var sqlStr3 = "insert into impact values(?,?,?,?,?,?),(?,?,?,?,?,?)";
                    db.all(sqlStr1, [req.session.username, '2_1', req.query.n21, req.query.n14, req.query.n7, req.query.fam1], function (err) {
                        if (err == null) {
                            db.all(sqlStr2, [req.session.username, '2_2', req.query.g147, req.query.g77, req.query.g07, req.query.gno, req.query.fam2], function (err) {
                                if (err == null) {
                                    db.all(sqlStr3, [req.session.username, '2_1', req.query.analysis, req.query.experience, req.query.know, req.query.intuition, req.session.username, '2_2', req.query.analysis2, req.query.experience2, req.query.know2, req.query.intuition2], function (err) {
                                        if (err == null)
                                            res.send('1');
                                        else {
                                            res.send('0');
                                            console.log(err);
                                        }

                                    });
                                } else
                                    console.log(err);
                            });
                        } else
                            console.log(err);
                    });
                }
            });
        } catch (ex) {
            console.log(ex);
        }
    } else if (mode == '3') { //第三轮调查 && 第四轮调查
        var index = req.session.index;
        var sqlStr = "select * from answer" + index + " where expert_id = ?";
        try {
            db.all(sqlStr, [req.session.username], function (err, rows) {
                if (rows.length != 0) {
                    var sqlStr1 = "UPDATE answer" + index + " SET byes = ?,b10=?,b12=?,bno=?,fam=? where expert_id = ?";
                    var sqlStr2 = "UPDATE impact SET analysis = ?,experience=?,know=?,intuition=? where expert_id = ? and q_no=?";
                    db.all(sqlStr1, [req.query.byes, req.query.b10, req.query.b12, req.query.b10, req.query.fam, req.session.username], function (err) {
                        if (err == null) {
                            db.all(sqlStr2, [req.query.analysis, req.query.experience, req.query.know, req.query.intuition, req.session.username, index], function (err) {
                                if (err == null)
                                    res.send('1');
                                else {
                                    res.send('0');
                                    console.log(err);
                                }
                            });
                        } else
                            console.log(err);
                    });

                } else {
                    var sqlStr1 = sqlStr1 = "insert into answer" + index + " values(?,?,?,?,?,?,?,?) ";
                    var sqlStr2 = "insert into impact values(?,?,?,?,?,?)";
                    db.all(sqlStr1, [req.session.username, index, req.query.byes, req.query.b10, req.query.b11, req.query.b12, req.query.bno, req.query.fam], function (err) {
                        if (err == null) {
                            db.all(sqlStr2, [req.session.username, index, req.query.analysis, req.query.experience, req.query.know, req.query.intuition], function (err) {
                                if (err == null)
                                    res.send('1');
                                else {
                                    res.send('0');
                                    console.log(err);
                                }
                            });
                        } else
                            console.log(err);
                    });
                }
            });
        } catch (ex) {
            console.log(ex);
        }
    }

});

module.exports = router;